package com.lucker.service.evenydayopentime;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.lucker.cds.mapper.CEvenyDayOpenTimeMapper;
import com.lucker.cds.model.CEvenyDayOpenTime;
import com.lucker.common.util.DateTimeUtil;
import com.lucker.ds.model.EvenyDayOpenTime;
import com.lucker.service.BaseServiceImpl;

@Service
public class EvenyDayOpenTimeServiceImpl extends BaseServiceImpl<EvenyDayOpenTime, CEvenyDayOpenTime, Long> implements EvenyDayOpenTimeService {

	@Override
	public CEvenyDayOpenTimeMapper getCustomMapper() {
		return (CEvenyDayOpenTimeMapper)this.getBaseCustomMapper();
	}
	
	public CEvenyDayOpenTime getCEvenyDayOpenTime(CEvenyDayOpenTime cEvenyDayOpenTime) {
		if(cEvenyDayOpenTime == null) return null;
		if(cEvenyDayOpenTime.getCaiId() == null || cEvenyDayOpenTime.getCaiId() < 1) {
			return null;
		}
//		if(StringUtils.isBlank(cEvenyDayOpenTime.getIssuenoPrefix())) {
//			return null;
//		}
		cEvenyDayOpenTime.setIssuenoPrefix(DateTimeUtil.getDate(new Date(), "yyMMdd"));
		List<CEvenyDayOpenTime> list = getCustomMapper().selectNextOpenTime(cEvenyDayOpenTime);
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}else {//这个查询会有空窗期,如果每天四点做初始化，那么，前一天最后一期结束的时间，到这个4点前，这一段时间就是控窗期，查不到数据
			EvenyDayOpenTime query = new EvenyDayOpenTime();
			query.setCaiId(cEvenyDayOpenTime.getCaiId());
			Date nextDay = DateTimeUtil.addDay(new Date(), 1);
			query.setIssuenoPrefix(DateTimeUtil.getDate(nextDay, "yyMMdd"));
			query.orderById(true);
			List<EvenyDayOpenTime> list2 = queryAll(query);
			if(CollectionUtils.isNotEmpty(list2)) {
				EvenyDayOpenTime opentime = list2.get(0);
				CEvenyDayOpenTime copen = new CEvenyDayOpenTime();
				copen.setCaiId(opentime.getCaiId());
				copen.setIssueno(opentime.getIssueno());
				copen.setIssuenoPrefix(opentime.getIssuenoPrefix());
				copen.setOpenDate(opentime.getOpenDate());
				return copen;
			}else {
				return null;
			}			
		}
		
		
		
	}
	
	
	public CEvenyDayOpenTime selectPrevOpenTime(CEvenyDayOpenTime cEvenyDayOpenTime) {
		if(cEvenyDayOpenTime == null) return null;
		if(cEvenyDayOpenTime.getCaiId() == null || cEvenyDayOpenTime.getCaiId() < 1) {
			return null;
		}
		cEvenyDayOpenTime.setIssuenoPrefix(DateTimeUtil.getDate(new Date(), "yyMMdd"));
		List<CEvenyDayOpenTime> list = getCustomMapper().selectNextOpenTime(cEvenyDayOpenTime);
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}else {
			return null;
		}
	}

	

}
